package com.biz.primus.ms.commodity.dao.po;


import com.biz.primus.base.enums.commodity.IStatus;
import com.biz.primus.base.enums.SaleChannel;
import com.biz.primus.commodity.vo.backend.ICategoryVo;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 分类 Po
 *
 * @author david-liu
 * @date 2016年12月14日
 * @reviewer
 * @see
 */
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "pro_category", uniqueConstraints = {@UniqueConstraint(columnNames="name")})
public class Category extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 5027873885402573782L;

    /**
     * 分类名称
     */
    @Column(nullable = false, length = 50)
    private String name;

    /**
     * 分类排序
     */
    @Column
    private Integer idx;

    /**
     * 分类图标
     */
    @Column
    private String logo;

    /**
     * 分类等级
     */
    @Column
    private Integer level;

    /**
     * 是否展示在前端
     */
    @Convert(converter = IStatus.Converter.class)
    @Column(nullable = false)
    private IStatus status;

    /**
     * 父分类
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "parent_id")
    private Category parent;

    /**
     * 子分类
     */
    @OrderBy(value = "idx")
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Category> children;

    /**
     * 品牌
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @OrderBy(value = "idx")
    @JoinTable(name = "pro_category_brand",
            joinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "brand_id", referencedColumnName = "id")},
            uniqueConstraints = {@UniqueConstraint(columnNames= {"category_id" , "brand_id"})})
    private List<Brand> brands;

    /**
     * 分类商品
     */
    @OneToMany
    private List<Product> products;

    /**
     * 分类商品审核记录
     */
    @OneToMany
    private List<ProductAudit> productAudits;

    /**
     * SEO 标题
     */
    @Column(length = 50)
    private String seoTitle;

    /**
     * SEO 关键字
     */
    @Column(length = 50)
    private String seoKeywords;

    /**
     * SEO 描述
     */
    @Column
    private String seoDescription;

    @Enumerated(EnumType.STRING)
    private SaleChannel saleChannel;


    /**
     * 删除标识(默认是未删除)
     */
    @Column
    private Boolean deleteFlag = Boolean.FALSE;

    /**
     * 通过 VO 获取 Po(未设置 ID 和关联对象)
     *
     * @param categoryVo Vo
     */
    public void fromVo(ICategoryVo categoryVo) {
        this.setName(categoryVo.getName());
        this.setLogo(categoryVo.getLogo());
        this.setStatus(categoryVo.getStatus());
        this.setSeoTitle(categoryVo.getSeoTitle());
        this.setSeoKeywords(categoryVo.getSeoKeywords());
        this.setSeoDescription(categoryVo.getSeoDescription());
        this.setSaleChannel(categoryVo.getSaleChannel());
    }

}
